package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Article;
import ysq.xyg.model.info.ArticleInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.ArticleService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * 文章管理
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {


    private static Logger log= LoggerFactory.getLogger(ArticleController.class);

    @Resource
    private ArticleService articleService;

    private Article article;

    @RequestMapping("/listPage")
    public ModelAndView List(@RequestParam String type){
        return new ModelAndView("/articleList").addObject("type",type);
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Article article,Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Article> pageInfo = articleService.pageInfo(article, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (Article item : pageInfo.getList()) {
                row = new JSONObject();
                row.put("id", item.getId());
                row.put("name", item.getName());
                row.put("type", item.getType());
                row.put("orderNum", item.getOrderNum());
                row.put("status", item.getStatus());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(),DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id,String type){
        ModelAndView view = new ModelAndView("/articleUpdateOrSave");
        article = articleService.selectByPrimaryKey(id);
        view.addObject("article", article);
        view.addObject("type", type);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(ArticleInfo articleInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(articleInfo.getId())) {
            article = new Article();
            article.setId(BaseUtil.uId());
            article.setName(articleInfo.getName());
            article.setDescription(articleInfo.getDescription());
            article.setType(articleInfo.getType());
            article.setOrderNum(articleInfo.getOrderNum());
            article.setStatus(articleInfo.getStatus());
            article.setCreateTime(new Date());

            int id = articleService.insert(article);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            article = articleService.selectByPrimaryKey(articleInfo.getId());
            article.setName(articleInfo.getName());
            article.setDescription(articleInfo.getDescription());
            article.setType(articleInfo.getType());
            article.setOrderNum(articleInfo.getOrderNum());
            article.setStatus(articleInfo.getStatus());
            article.setCreateTime(new Date());
            int id = articleService.updateByPrimaryKey(article);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
           int ids= articleService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
